//
//  LOGoodsDetailViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/10/25.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "LOGoodsDetailViewController.h"
#import "LOShopCarViewController.h"
#import "LOGoodsDetailPayViewController.h"
#import "LOCarDetailViewController.h"

@interface LOGoodsDetailViewController ()<WKNavigationDelegate,WKUIDelegate,WKScriptMessageHandler>
@property (strong, nonatomic) WKWebView *WKWeb;

@end

@implementation LOGoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"商品详情";
    
    WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc]init];
    //注册js方法
    config.userContentController = [[WKUserContentController alloc]init];
    _WKWeb = [[WKWebView alloc] initWithFrame:CGRectMake(0, 70, WIDTH, HEIGHT-70) configuration:config];
    _WKWeb.navigationDelegate = self;
    _WKWeb.UIDelegate = self;
    [self.view addSubview:_WKWeb];
    [_WKWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?userToken=%@&goodsId=%@&type=0", API(LOGoodsDetail), [Utils getUserToken], self.goodsId]]]];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_WKWeb.configuration.userContentController addScriptMessageHandler:self name:@"cart"];
    [_WKWeb.configuration.userContentController addScriptMessageHandler:self name:@"set"];
    [_WKWeb.configuration.userContentController addScriptMessageHandler:self name:@"buy"];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_WKWeb.configuration.userContentController removeScriptMessageHandlerForName:@"cart"];
    [_WKWeb.configuration.userContentController removeScriptMessageHandlerForName:@"set"];
    [_WKWeb.configuration.userContentController removeScriptMessageHandlerForName:@"buy"];
}
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    completionHandler();
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSLog(@"%@",navigationAction.request.URL);
    decisionHandler(WKNavigationActionPolicyAllow);
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    [self.view loadingOnAnyView];
}

// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [self.view removeAnyView];
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"set"]) {
        
        if ([message.body isEqual:@"gwc"]) {
            LOShopCarViewController *shopcar = [[LOShopCarViewController alloc]init];
            [self.navigationController pushViewController:shopcar animated:YES];
        }
    }else if([message.name isEqualToString:@"cart"]){
        NSDictionary *dic = message.body;
        
        [THWManager PostHttp:API(LOAddShoppingCar) Parameters:dic Success:^(id responseObject) {
            
            [self.view Message:responseObject[@"msg"]];
            [_WKWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?userToken=%@&goodsId=%@&type=0", API(LOGoodsDetail), [Utils getUserToken], self.goodsId]]]];
        } andFalse:^(NSError *error) {
            
        }];
    }else if([message.name isEqualToString:@"buy"]){
        
        LOCarDetailViewController *detail = [[LOCarDetailViewController alloc]init];
        detail.payDic = message.body;
        
        [self.navigationController pushViewController:detail animated:YES];
    }
}
//-(void)returnBack
//{
//    if ([_WKWeb canGoBack]) {
//        [_WKWeb goBack];
//    }else{
//        [self.navigationController popViewControllerAnimated:YES];
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
